#include <iostream>
#include <cmath>

using ll = long long;

ll p, q, goal;

int calc_h(ll x)
{
	int t = std::log2(x);
	ll re = x - (1 << t) + 1;
	return t + std::log2(re);
}

bool IDA(int d, int maxd, ll now, ll last)
{
	if (d == maxd) return now == goal;
	if (now <= goal) return goal - now + d <= maxd;

	int h = 0;
	if (last) h = calc_h(((now - goal) / last));
	else h = calc_h((now - goal));

	if (d + h > maxd) return false;
	// keep down
	if (last) {
		if (now - last * 2 >= q) {
			if (IDA(d + 1, maxd, now - last * 2, last * 2)) return true;
		} else {
			if (d + 1 + q - (now - last * 2) <= maxd) return true;
		}
	} else {
		if (now - 1 >= q) {
			if (IDA(d + 1, maxd, now - 1, 1)) return true;
		} else {
			if (d + 1 + q - (now - 1) <= maxd) return true;
		}
	}

	// break
	if (IDA(d + 1, maxd, now, 0)) return true;
	return false;
}

int main()
{
	std::ios_base::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		std::cin >> p >> q;
		if (p <= q) { std::cout << q - p << "\n"; continue; }

		goal = q;
		for (int i = 0; ; i++) {
			std::cerr << i << "\n";
			if (IDA(0, i, p, 0)) {
				std::cout << i << "\n";
				break;
			}
		}
	}
}

